package com.lanou.dllo.soccernews.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.lanou.dllo.soccernews.R;
import com.lanou.dllo.soccernews.bean.CircleArticlesEntity;
import com.lanou.dllo.soccernews.utils.CircleOnClickListener;
import com.lanou.dllo.soccernews.utils.CircleOnClickListener;
import com.lanou.dllo.soccernews.volley.GetVolleyDatasUtil;

import java.util.ArrayList;
import java.util.List;

import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;

/**
 * Created by dllo on 15/10/24.
 */
public class CircleViewAdapte extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private List<CircleArticlesEntity> articlesEntities;
    private static final int TYPE_ITEM = 0;
    private static final int TYPT_FOOTER = 1;
    private CircleOnClickListener listener;

    public void setOnCircleOnClickListener(CircleOnClickListener listener){
        this.listener = listener;
    }

    public void addRefreshData(List<CircleArticlesEntity> articlesEntities) {
        if (this.articlesEntities == null) {
            this.articlesEntities = new ArrayList<>();
        }
        this.articlesEntities.clear();
        this.articlesEntities.addAll(articlesEntities);
        notifyDataSetChanged();
    }

    public void addLoadData(List<CircleArticlesEntity> articlesEntities) {
        if (this.articlesEntities == null) {
            this.articlesEntities = new ArrayList<>();
        }
        this.articlesEntities.addAll(articlesEntities);
        notifyDataSetChanged();
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());

        if (viewType == TYPE_ITEM) {
            View view = inflater.inflate(R.layout.item_circlerecycleview, null);

            return new  MyViewHolder(view,listener);
        } else if (viewType == TYPT_FOOTER) {
            View view = inflater.inflate(R.layout.foot_recyclerview, null);
            view.setLayoutParams(new ViewGroup.LayoutParams(MATCH_PARENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT));
            return new FooterViewHolder(view);
        }

        return null;
    }


    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        //设置参数

        if (holder instanceof MyViewHolder) {
            if (articlesEntities != null && articlesEntities.size() > 0) {


                CircleArticlesEntity articles = articlesEntities.get(position);
                CircleArticlesEntity.TopicEntity topicEntity = articles.getTopic();
                CircleArticlesEntity.TopicEntity.GroupEntity groupEntity = topicEntity.getGroup();
                CircleArticlesEntity.TopicEntity.AuthorEntity authorEntity = topicEntity.getAuthor();
                ((MyViewHolder) holder).position = position;
                ((MyViewHolder) holder).ci_content.setText(topicEntity.getContent());
                //  Log.i("lanou", articles.getComments_total() + "长度");
                ((MyViewHolder) holder).ci_title.setText(groupEntity.getTitle());
                ((MyViewHolder) holder).ci_username.setText(authorEntity.getUsername());
                ((MyViewHolder) holder).ci_comments_total.setText(articles.getComments_total() + "");
                if (topicEntity.getThumb() != null && topicEntity.getThumb().length() > 0) {
                    String imgUrl = topicEntity.getThumb();
                    // GetVolleyDatasUtil.getBitmapRequest(imgUrl, holder.ci_thump);
                    GetVolleyDatasUtil.getNetWorkImg(imgUrl, ((MyViewHolder) holder).ci_thump);
                }

            }
        }
//        if (holder instanceof  FooterViewHolder){
//
//        }
    }

    @Override
    public int getItemCount() {
        return articlesEntities != null && articlesEntities.size() > 0 ? articlesEntities.size() + 1
                : 0;
    }

    public  class MyViewHolder extends RecyclerView.ViewHolder implements
            View.OnClickListener{
        private int position = 0;

        private CircleOnClickListener mListener;

        private TextView ci_content, ci_username, ci_title, ci_comments_total;

        private ImageView ci_thump;

        public MyViewHolder(View itemView,CircleOnClickListener listener) {
            super(itemView);
            //初始化
            ci_content = (TextView) itemView.findViewById(R.id.ci_content);
            ci_username = (TextView) itemView.findViewById(R.id.ci_username);
            ci_title = (TextView) itemView.findViewById(R.id.ci_title);
            ci_comments_total = (TextView) itemView.findViewById(R.id.ci_comments_total);

            ci_thump = (ImageView) itemView.findViewById(R.id.ci_thumb);
            this.mListener = listener;
            itemView.setOnClickListener(this);

        }

        @Override
        public void onClick(View v) {
            if (mListener != null) {
                mListener.onItemClickCircle(articlesEntities.get(position));
            }

        }
    }

    @Override
    public int getItemViewType(int position) {


        if (position + 1 == getItemCount()) {
            return TYPT_FOOTER;
        } else {
            return TYPE_ITEM;
        }
    }

    class FooterViewHolder extends RecyclerView.ViewHolder {

        TextView text;
        ProgressBar progressBar;

        public FooterViewHolder(View view) {

            super(view);

            text = (TextView) view.findViewById(R.id.pulltext);
            progressBar = (ProgressBar) view.findViewById(R.id.pullprogress);


        }
    }
}
